package com.jstyle.jclife.adapter;

import android.annotation.SuppressLint;
import android.graphics.drawable.Drawable;
import android.support.constraint.ConstraintLayout;
import android.support.constraint.ConstraintSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.Space;
import android.widget.TextView;

import com.jstyle.jclife.R;
import com.jstyle.jclife.model.PaceData;
import com.jstyle.jclife.utils.DateUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/8/6.
 */

public class PaceAdapter extends RecyclerViewBaseAdapter<PaceData> {


    float maxPace;
    float minPace;
    public PaceAdapter(List mDatas) {
        super(mDatas);
    }

    public PaceAdapter(List mDatas, float maxPace, float minPace) {
        super(mDatas);
        this.maxPace=maxPace;
        this.minPace=minPace;
    }
    @SuppressLint("SetTextI18n")
    @Override
    protected void bindData(BaseViewHolder holder, int position) {
        PaceViewHolder paceViewHolder = (PaceViewHolder) holder;
        ConstraintSet constraintSet = new ConstraintSet();
        constraintSet.clone(paceViewHolder.ConstraintLayoutPace);
        float weight=getWidth(paceViewHolder.ConstraintLayoutPace,position);
        float pace=  mDataList.get(position).getPace();
        int id= R.drawable.home_progressbar;
        if(pace==minPace){
            id= R.drawable.maxpace_progressbar;
        }
        Drawable progressDrawable=holder.itemView.getContext().getResources().getDrawable(id);
        paceViewHolder.progressBarPace.setProgressDrawable(progressDrawable);
        if(weight<1.5)weight=1.5f;
        constraintSet.setHorizontalWeight(R.id.bt_pace_count, 1);
        constraintSet.setHorizontalWeight(R.id.space, 9-weight);
        constraintSet.setHorizontalWeight(R.id.progressBar_pace, weight);
        constraintSet.applyTo(paceViewHolder.ConstraintLayoutPace);
        paceViewHolder.btPaceCount.setText((position + 1) + "");


        if(mDataList.get(position).isDistanceFinish()){
            paceViewHolder.tvPaceValue.setText(DateUtil.getPaceTime((int) pace));
        }else{
            long time=mDataList.get(position).getTime();
            paceViewHolder.tvPaceValue.setText(holder.itemView.getContext().getString(R.string.Lessthan1km)+ DateUtil.getPaceTime((int) time));
        }

    }

    private static final String TAG = "PaceAdapter";
    private float getWidth(ConstraintLayout constraintLayout,int position){
        PaceData paceData=mDataList.get(position);
        float pace= paceData.getPace() ;

        return paceData.isDistanceFinish()?pace/maxPace*8:5;
    }
    @Override
    protected int getLayoutId(int viewType) {
        return R.layout.item_pace;
    }

    public class PaceViewHolder extends BaseViewHolder {
        @BindView(R.id.progressBar_pace)
        ProgressBar progressBarPace;
        @BindView(R.id.bt_pace_count)
        Button btPaceCount;
        @BindView(R.id.ConstraintLayout_pace)
        ConstraintLayout ConstraintLayoutPace;
        @BindView(R.id.tv_pace_value)
        TextView tvPaceValue;
        @BindView(R.id.space)
        Space space;
        public PaceViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(getLayoutId(viewType), parent, false);
        return new PaceViewHolder(view);
    }
}
